use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use validator::Validate;

#[derive(Debug, Clone)]
pub struct User {
    pub id: String,
    pub email: String,
    pub password: String,
    pub create_at: NaiveDateTime,
    pub update_at: Option<NaiveDateTime>,
}

#[derive(Debug, Clone, Serialize)]
pub struct FilterUser {
    pub id: String,
    pub email: String,
    pub create_at: String,
    pub update_at: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Validate)]
pub struct RegisterUser {
    #[validate(length(min = 1, message = "Email is required"))]
    #[validate(email(message = "Email is invalid"))]
    pub email: String,

    #[validate(length(min = 1, message = "Password is required"))]
    pub password: String,

    #[validate(must_match(other = "password", message = "Passwords do not match"))]
    pub password_confirm: String,
}

#[derive(Debug, Clone, Deserialize, Validate)]
pub struct LoginUser {
    #[validate(length(min = 1, message = "Email is required"))]
    #[validate(email(message = "Email is invalid"))]
    pub email: String,

    #[validate(length(min = 1, message = "Password is required"))]
    pub password: String,
}

#[derive(Debug, Serialize)]
pub struct LoginResult {
    pub token: String,
}

impl From<User> for FilterUser {
    fn from(u: User) -> Self {
        Self {
            id: u.id,
            email: u.email,
            create_at: u.create_at.format("%Y-%m-%d %H:%M:%S").to_string(),
            update_at: u
                .update_at
                .map(|v| v.format("%Y-%m-%d %H:%M:%S").to_string()),
        }
    }
}
